<?php

namespace common\models;

use common\helpers\TransformApi;
use Yii;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string|null $nickname 昵称
 * @property string|null $headimgurl 头像
 * @property string|null $openid openid
 * @property string|null $unionid unionid
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property int|null $status 状态
 * @property string|null $info 信息
 * @property string|null $start_time 会员开始时间
 * @property string|null $end_time 会员结束时间
 */
class User extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['headimgurl', 'openid', 'unionid', 'info'], 'string'],
            [['create_time', 'update_time', 'start_time', 'end_time'], 'safe'],
            [['status'], 'integer'],
            [['nickname'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'nickname' => '昵称',
            'headimgurl' => '头像',
            'openid' => 'openid',
            'unionid' => 'unionid',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'status' => '状态',
            'info' => '信息',
            'start_time' => '会员开始时间',
            'end_time' => '会员结束时间',
        ];
    }

    /**
     * 用户是不是会员
     * @param $userid
     * @param $start_time
     * @param $end_time
     * @param int $istime
     * @return bool
     */
    public function isHuiyuan($userid,$start_time,$end_time,$istime=1)
    {
        if (!$userid){
            return false;
        }
        if (!$istime){
            $user = User::find()->where(['id'=>$userid])->select(['start_time','end_time'])->asArray()->one();
            $start_time = $user['start_time']??'';
            $end_time = $user['end_time']??'';
        }
        if($start_time&&$end_time){
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $res = TransformApi::testingTimeRange($start_time,$end_time);
        }else{
            $res = false;
        }
        return $res;
    }

    /**
     * 获取用户html
     * @param $data
     * @return array|string
     */
    public function getUserHtml($data)
    {
        if (is_array($data)){
            $arr = [];
            foreach ($data as $datum){
                $info = json_decode($datum['info'],true);
                $arr[$datum['id']] = "<div><div><img style='width: 100px' src='{$info['avatarUrl']}' alt=''></div><div>{$info['nickName']}</div></div>";
            }
            return $arr;
        }else{
            $info = json_decode($data,true);
            $html = "<div><div><img style='width: 100px' src='{$info['avatarUrl']}' alt=''></div><div>{$info['nickName']}</div></div>";
            return $html;
        }
    }
}
